package controllers

import (
	"github.com/gin-gonic/gin"
	"my-gin-tiktok/app/daos"
	"my-gin-tiktok/app/services"
	"my-gin-tiktok/common/response"
	"strconv"
)

// CommentAction 视频评论或者取消评论
func CommentAction(ctx *gin.Context) {
	var form daos.ActionComment
	if err := ctx.ShouldBind(&form); err != nil {
		response.ValidateFail(ctx, err.Error())
		return
	}

	actionType := form.ActionType
	if actionType != 1 && actionType != 2 {
		response.ValidateFail(ctx, "动作标识错误")
		return
	}

	var err error
	userID, _ := strconv.ParseInt(ctx.Keys["id"].(string), 10, 64)
	switch form.ActionType {
	case 1:
		err = services.CommentService.CreateComment(ctx, userID, form.VideoId, form.CommentText)
	case 2:
		err = services.CommentService.DeleteComment(ctx, userID, form.VideoId)
	}
	if err != nil {
		response.BusinessFail(ctx, err.Error())
		return
	}
	response.Success(ctx, nil)
}

// CommentList 视频评论列表
func CommentList(ctx *gin.Context) {
	var form daos.VideoCommentListReq
	if err := ctx.ShouldBind(&form); err != nil {
		response.ValidateFail(ctx, err.Error())
		return
	}

	commentList, err := services.CommentService.GetVideoComments(ctx, form.VideoId, 0, 10)
	if err != nil {
		response.BusinessFail(ctx, err.Error())
		return
	}
	response.Success(ctx, commentList)
}
